import QtQuick 1.0

//full screen interstitial ad
//(well should be used fullscreen but you need to specify width and height still
//options:
//      message text: something like "A quick word from our sponsors
//      color: background color for panel
//      skip in: number of seconds before ad can be skipped

Rectangle {
    id: adWrapper

    //specific to interstitials
    //default to a boring gray
    color: "#c0c0c0"
    property alias messageText: sponsorText.text
    property int skipIn:  0

    //screen width is important for getting the right ads showing
    property alias screenWidth: ad.screenWidth
    property alias screenHeight: ad.screenHeight

    //private
    property int __skippingIn:  -1

    //aliased to the ad properties so they can be set here but reflected in the child
    property alias appid: ad.appid

    //these probably don't need to be specified
    property alias channelid: ad.channelid



    function loadAd() {
        ad.loadAd();
        //disable skip button and restart timer
        if(skipIn > 0) {
            __skippingIn = skipIn
            skipButton.disabled = true;
            skipTimer.start();
        }

    }

    function hide() {
       visible = false;
    }

    function show() {
        visible = true;
    }


    Text {
        id: sponsorText
        text: "A quick word from our sponsors..."
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 25
        font.pointSize: 14
        wrapMode: Text.Wrap
        color: "black"
    }

    Column {
        id: buttons
        spacing: 6
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom:  parent.bottom
        anchors.bottomMargin: 25
        //width: parent.width
        //height:  200

        //skip button
        Rectangle {
            id: skipButton
            property bool disabled: false
            border.width: 1
            radius: 2
            border.color:  "#060606"
            color: "#cfcfcf"
            //color: "green"
            width: adWrapper.width * .70
            //width: 200
            height: 40
            Text {
                color: skipButton.disabled ? "#e0e0e0" : "black"
                font.pointSize: 14
                anchors.centerIn: parent
                text: __skippingIn > 0 ? "Skip in " + __skippingIn + " sec(s)" : "Skip"
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("skip pressed");
                    if(parent.disabled == false)
                        adWrapper.hide();

                }

                onPressed: { if(!parent.disabled) parent.color = Qt.lighter(parent.color,1.1); }
                onReleased: { if(!parent.disabled)  parent.color = Qt.darker(parent.color, 1.1); }
            }

        }


        //visit link button
        Rectangle {
            border.width: 1
            radius: 2
            border.color:  "#060606"
            color: "#cfcfcf"
            //color: "green"
            width: adWrapper.width * .70
            //width: 200
            height: 40
            Text {
                color: "black"
                font.pointSize: 14
                anchors.centerIn: parent
                text: "Open Link"
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("visit ad button pressed");
                    ad.adClicked();

                }

                onPressed: { parent.color = Qt.lighter(parent.color,1.2); }
                onReleased: { parent.color = Qt.darker(parent.color, 1.2); }
            }

        }
    }


    InnerActiveAd {
        id: ad
        width: parent.width
        anchors.bottom: buttons.top
        anchors.bottomMargin: 10
        anchors.top:  sponsorText.bottom
        anchors.topMargin: 10

    }

    Timer {
        id: skipTimer
        interval: 1000      //countdown timer
        repeat: true

        onTriggered: {
            if(__skippingIn == 0) {
                skipTimer.stop();
                console.log("Ad finished");
                skipButton.disabled = false;

            } else
                __skippingIn = __skippingIn - 1;
        }


    }
}
